//
//  LoadView.m
//  OASystem
//
//  Created by mac on 14-5-14.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "LoadView.h"

@interface LoadView ()

@end

@implementation LoadView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //是否更新
    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://10.101.1.10:8080/update.json"]];
    [request setDelegate:self];
    //    [request setTimeOutSeconds:5];
    [request setDidFinishSelector:@selector(urlRequestSucceeded:)];
    [request setDidFailSelector:@selector(urlRequestFailed:)];
    [request startAsynchronous];

}

-(void)urlRequestSucceeded:(ASIHTTPRequest *)request
{
    NSError *error;
    NSData *data=[request responseData];
    NSDictionary * dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
    
    if(dic != nil)
    {
        NSString *nowVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
        NSLog(@"nowVersion--->%@",nowVersion);
        NSLog(@"iosversion--->%@",[dic objectForKey:@"iosversion"]);
        
        if(![nowVersion isEqualToString:[dic objectForKey:@"iosversion"]])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                            message:@"有新版本进行更新"
                                                           delegate:self
                                                  cancelButtonTitle:@"下次更新"
                                                  otherButtonTitles:@"更新", nil];
            [alert show];
            updateUrlStr = [dic objectForKey:@"iosurl"];
        }
        else
        {
            [self push];
        }
    }
}

- (void)push
{
    LoginView *instance = [[LoginView alloc] init];
    [self.navigationController pushViewController:instance animated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
        [self getUpdate:updateUrlStr];
    else
    {
        [self push];
    }
}

-(void)urlRequestFailed :(ASIHTTPRequest *)request
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
    
    NSError *error=[request error];
    NSLog(@"%@",error);
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":@"获取版本信息失败"}];
    
    [self push];
}


//获取更新
- (void)getUpdate:(NSString *)urlStr
{
    NSLog(@"urlStr--->%@",urlStr);
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    str= [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@",str];
    
    if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]])
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":@"获取更新失败"}];
    else
    {
        LoginView *instance = [[LoginView alloc] init];
        [self.navigationController pushViewController:instance animated:YES];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
